package cn.ffcs.security.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.ffcs.base.service.BaseService;
import cn.ffcs.organ.dao.OrgRelaPersonDAO;
import cn.ffcs.organ.dao.PersonDAO;
import cn.ffcs.security.dao.IRoleRelaUserDAO;
import cn.ffcs.security.dao.IUserDAO;
import cn.ffcs.security.dao.RoleRelaUserDAO;
import cn.ffcs.security.dao.UserDAO;
import cn.ffcs.security.model.Role;
import cn.ffcs.security.model.RoleRelaUser;
import cn.ffcs.security.model.User;
import cn.ffcs.util.context.ApplicationContextUtil;
import cn.ffcs.util.ext.FieldObject;

/**
 * 
 * 角色服务层接口实现<br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author liuzhh
 * @version $Id$
 */
public class RoleServiceForOuter<R extends Role> extends BaseService<R> implements
        IRoleService<R>
{
    /** 用户角色接口 **/
    private IRoleRelaUserDAO roleRelaUserDAO;
    /** 用户接口 **/
    private IUserDAO userDAO;
    
    public List<FieldObject> getCurrRolePersons(String roleID){
        // 获取session信息
        ServletContext sc = ApplicationContextUtil.getHttpServletRequest().getSession().getServletContext();
        WebApplicationContext wac = WebApplicationContextUtils
                .getWebApplicationContext(sc);
        //获取注册ｂｅａｎ
        this.roleRelaUserDAO=(RoleRelaUserDAO)wac.getBean("RoleRelaUserDAO");
        this.userDAO=(UserDAO)wac.getBean("UserDAO");
        
        //获取展示列表对象
        List<FieldObject> result=new ArrayList<FieldObject>();
        
        //获取角色下所有用户
        Criterion cond1 = Restrictions.eq("role.id", Integer.parseInt(roleID));  
        List<RoleRelaUser> rrus=this.roleRelaUserDAO.findBy(cond1);
        
        if (rrus!=null && rrus.size()>0){
            for (RoleRelaUser rru:rrus){
                User user=rru.getUser();
                
                User u=(User)this.userDAO.findById(user.getId());

                FieldObject fo=new FieldObject();
                //设置显示名称
                fo.setName(u.getPerson().getName());
                //查找对应用户ＩＤ
//                fo.setId(u.getId());
                fo.setId(u.getPerson().getId());
                
                result.add(fo);
            }
        }
        
        return result;
    }
     
}
